﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fitment
{
    public partial class ModifyTime : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int userID = Convert.ToInt32(Session["User_ID"]);   //用于获取用户id
                MyLinqDataContext data = new MyLinqDataContext();   //使用linq进行查询
                var search = (from p in data.ProjectInfo
                              from q in data.HouseInfo
                              where p.House_ID == q.House_ID && q.User_ID == userID && Convert.ToDateTime(p.StartDate) != null
                              select q.House_ID).Distinct();
                if (search.Count() == 0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('你还没有进行日期选择，请指定工程开始时间！');</script>");
                    return;
                }
                this.house.DataSource = search.ToList();   //选择数据源
                this.house.DataBind();   //绑定数据源
                bindList();  //重新绑定数据库连接
            }
        }

         private DS data = new DS(); //建立数据库连接
        /// <summary>
        /// 用于绑定数据库
        /// </summary>
        protected void bindList()
        {
            int id = Convert.ToInt32(Request.Cookies["userID"].Value);   //用于获取用户id
            string strSql = "SELECT Pro_ID,HouseInfo.House_ID,StartDate,FinishDate,MatName,TotalPrice FROM ProjectInfo,Material,HouseInfo WHERE User_ID='" + 
                id + "' AND ProjectName=ProName AND ProjectInfo.House_ID=HouseInfo.House_ID order by HouseInfo.House_ID";
            DataTable table = data.GetDataSource(strSql,"Query");
            this.GridView1.DataSource = table.DefaultView;
            GridView1.DataBind();
        }

        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox start=this.GridView1.Rows[e.RowIndex].FindControl("startTime") as TextBox;
            TextBox finish = this.GridView1.Rows[e.RowIndex].FindControl("finishTime") as TextBox;
            DateTime strStart = Convert.ToDateTime(start.Text.ToString());
            DateTime strFinish = Convert.ToDateTime(finish.Text.ToString());
            int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
            string strSql = "UPDATE ProjectInfo SET startDate='"+strStart+"',finishDate='"+strFinish+"' WHERE Pro_ID='"+id+"'";
            if (data.ExcuteSql(strSql))
            {
                this.GridView1.EditIndex = -1;
                bindList();
            }
            else
            { //当插入失败时返回
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('更新数据失败，请检查！');</script>");
                return;
            }

            
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
            string strSql = "DELETE FROM ProjectInfo WHERE Pro_ID='"+id+"'";
            if (data.ExcuteSql(strSql))
            {
                bindList();
                this.GridView1.EditIndex = -1;
            }
            else
                return;
        }

        /// <summary>
        ///翻页操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;  //当翻页时指定页号
            bindList();   //重新绑定数据
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            bindList();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            bindList();
        }

        /// <summary>
        /// 用于表示搜索功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void search_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(this.house.SelectedValue);   //用于获房间id
            string strSql = "SELECT Pro_ID,HouseInfo.House_ID,StartDate,FinishDate,MatName,TotalPrice FROM ProjectInfo,Material,HouseInfo WHERE ProjectInfo.House_ID='" +
                id + "' AND ProjectName=ProName AND ProjectInfo.House_ID=HouseInfo.House_ID order by HouseInfo.House_ID";
            DataTable _tb = data.GetDataSource(strSql, "query");
            this.GridView1.DataSource = _tb.DefaultView;
            this.GridView1.DataBind();
        }



    }
    
}
